package com.java2007.hotel.controller;

import com.java2007.hotel.factory.BeanFactory;
import com.java2007.hotel.pojo.Admin;
import com.java2007.hotel.service.AdminService;
import com.java2007.hotel.service.impl.AdminServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Date;

/**
 * 处理有关管理员的请求
 * @author csl
 * @version V1.0
 * @date 2021/01/21
 **/
@WebServlet("/adminServlet")
public class AdminServlet  extends BaseServlet {
    private AdminService adminService = (AdminService) BeanFactory.getBean("com.java2007.hotel.service.impl.AdminServiceImpl");

    /**
     * 处理登录的请求
     * @param req
     * @param resp
     */
    public String login(HttpServletRequest req, HttpServletResponse resp) {
        String adminName = req.getParameter("loginAdminName").trim();
        String adminPwd = req.getParameter("loginAdminPwd").trim();
        String verificationCode = req.getParameter("verificationCode").trim();
        HttpSession session = req.getSession();
        String validateCode = ((String) session.getAttribute("validateCode")).trim();

        if (validateCode.equalsIgnoreCase(verificationCode)){
            Admin admin = null;
            try {
                admin = adminService.loginAdmin(adminName, adminPwd);

            } catch (Exception e) {
                return "back:" + "用户或者密码错误%"+"index.jsp";
            }
            if (null != admin){
                session.setAttribute("admin",admin);
                return "redirect:"+req.getContextPath()+"/hotel/backend/index.jsp";
            }else {
                return "back:" + "用户或者密码错误%"+"index.jsp";
            }
        }else {
            return "back:" + "验证码错误%"+"index.jsp";
        }
    }

    /**
     * 处理注销管理员的请求
     * @param req
     * @param resp
     */
    public String delete(HttpServletRequest req, HttpServletResponse resp){
        HttpSession session = req.getSession();
        session.removeAttribute("admin");
        return "redirect:"+req.getContextPath()+"/hotel/backend/index.jsp";
    }

    /**
     * 处理注册管理员的请求
     * @param req
     * @param resp
     */
    public String add(HttpServletRequest req, HttpServletResponse resp) {
        String adminName = req.getParameter("adminName").trim();
        String adminPwd = req.getParameter("adminPwd").trim();
        String adminPhone = req.getParameter("adminPhone").trim();
        String adminEmail = req.getParameter("adminEmail").trim();
        String tempAdminRole = req.getParameter("adminRole").trim();
        Byte adminRole = Byte.valueOf(tempAdminRole);
        Date date = new Date();
        Admin admin = new Admin(null, adminName, adminPwd, (byte) 1, date, date, adminPhone, adminEmail, adminRole);

        System.out.println(admin);
        int i = 0;
        try {
            i = adminService.addAdmin(admin);
            System.out.println(i);
        } catch (SQLException e) {
            e.printStackTrace();
            return "toIndex";
        }
        if (i > 0){
            return "redirect:"+req.getContextPath()+"/index.jsp";
        }else {
            return "toIndex";
        }
    }
}
